/**
 * Created by easezhi on 2014/12/18.
 */
/*$('#userDg').datagrid({
    url:'/auth/users/userList',
    method:'post',
    columns:[
        [
            {field:'',checkbox:true},
            {field:'username',title:'用户名'},
            {field:'nickname',title:'昵称'},
            {field:'createtime',title:'注册时间'}
        ]
    ],
    idField:'id',

    sortName : 'createtime',//默认排序的列
    sortOrder: 'desc',
    rownumbers:true,
    striped: true,

    pagination : true,
    pageSize : 10,
    pageList : [ 10, 20, 40, 80 ],
    pagePosition:'bottom',

    toolbar:'#userTb'
})*/

/*显示数据网格*/
userGrid();
function userGrid(){
    $('#userDg').datagrid({
        url:'/auth/users/userList',
        method:'post',
        columns:[
            [
                {field:'',checkbox:true},
                {field:'username',title:'用户名',width:100},
                {field:'nickname',title:'昵称',width:100},
                {field:'phone',title:'手机',width:100},
                {field:'email',title:'邮箱',width:100},
                {field:'usertype',title:'用户类型',width:100,formatter:function(value){
                    switch (value){
                        case 1:return '管理员';
                        case 2:return '商家';
                        case 3:return '注册用户';
                        default :return '--';
                    }
                }},
                {field:'lname',title:'商家名称',width:100},
                {field:'integral',title:'积分',width:100,sortable:true,order:'desc'},
                {field:'carnum',title:'车牌号',width:100,sortable:true,order:'desc'},
                {field:'modelnum',title:'汽车型号',width:100,sortable:true,order:'desc'},
                {field:'insuranceDate',title:'保险到期日期',width:100,formatter:function(value){
                    if(value){
                        return value.substring(0, 10);
                    }
                }},
                {field:'createtime',title:'注册时间',width:100,sortable:true,order:'desc', formatter:function(value){
                    return value.substring(0, 10);
                }}
            ]
        ],
        idField:'id',
        toolbar:'#userTb',
        sortName : 'createtime',//默认排序的列
        sortOrder: 'desc',
        /*loader:function(param, success, error){
            alert(param);
        },*/

        rownumbers:true,
        striped: true,
        selectOnCheck:true,
        checkOnSelect:false,

        fitColumns:true,
        fit:true,

        pagination : true,
        pageNumber:1,
        pageSize : 10,
        pageList : [ 10, 20, 40, 80 ]
    });
}
function getSearchParams(){
    var params = {
        fromDate: $('#userFromDate').datebox('getValue'),
        toDate: $('#userToDate').datebox('getValue'),
        searchName: $('#userSearchName').textbox('getValue'),
        userType:$('#userType').combobox('getValue')
    };
    return params;
}

function searchUsers(){
    $('#userDg').datagrid('load', getSearchParams());
}

function editUser(){
    var user;
    var rows = $('#userDg').datagrid('getChecked');
    if(rows.length > 1 || rows.length == 0){
        $.messager.alert('提示', '请选择一项', 'error');
        return;
    }else{
        user = rows[0];
    }

    var option = {
        op:'edit',
        title:'编辑用户信息',
        msgTitle:'修改用户信息',
        user:user
    };
    loadUserDialog(option);
}
function addUser(){
    var option = {
        op:'add',
        title:'添加一条用户信息',
        msgTitle:'添加用户'
    };
    loadUserDialog(option);
}
function loadUserDialog(option){
    var userDialog = $('<div></div>').dialog({
        href:'/userManage/userAdd.html',
        width : 640,
        height : 520,
        title:option.title,
        maximizable:true,
        modal:true,
        cache:false,
        buttons:[
            {
                text : '取&nbsp;&nbsp;消',
                width : 60,
                handler : function() {
                    userDialog.dialog('close');
                }
            },
            {
                text : '保&nbsp;&nbsp;存',
                width : 60,
                handler : function() {
                    $('#userFm').form('submit', {
                        url:'/auth/users/userAddUpdate',
                        onSubmit: function(){
                            return $(this).form('validate');
                        },
                        success:function(data){
                            var result = eval('(' + data + ')');
                            if(result.state == 'success'){
                                userDialog.dialog('close');
                                //$('#userDg').datagrid('load');
                                searchUsers();
                                showMsg(option.msgTitle + '成功！');
                            }else{
                                showMsg(option.msgTitle + '失败！');
                            }
                        }
                    });
                }
            }
        ],
        onClose : function() {
            $(this).dialog('destroy');
        },
        onLoad: function () {
            if(option.op == 'edit'){
                $('#userFm').form('load', option.user);
            }
        }
    });
    return userDialog;
}


function delUser(){
    var rows = $('#userDg').datagrid('getChecked');
    if (rows.length > 0) {
        var msg;
        $.messager.confirm('确认', '提示：用户信息删除后将不能恢复，请确认！', function(b) {
            if (b) {
                var ids = [];
                for ( var i = 0; i < rows.length; i++) {
                    ids.push(rows[i].id);
                }
                $.ajax({
                    url : '/auth/users/delUser',
                    data : {
                        ids : ids.join()
                    },
                    type:'POST',
                    dataType : 'json',
                    success : function(data) {//alert(data)
                        var result = data;//eval('(' + data + ')');
                        if(result.state == 'success'){
                            $('#userDg').datagrid('reload');
                            $('#userDg').datagrid('uncheckAll');// 取消选中的所有记录
                            showMsg('删除成功！');
                        }else{
                            showMsg('删除失败！');
                        }
                    }
                });
            }
        });

    } else {
        $.messager.alert('提示', '还未选择任何项', 'error');
    }
}

/*扩展表单验证*/

$.extend($.fn.validatebox.defaults.rules,{
    equal:{
        validator:function(value, param){
            return value == $(param[0]).val();
        },
        message:'两次输入密码必须一致'
    },
    username:{
        validator:function(value, param){
            var reg = /^[a-zA-Z]\w{5,29}$/;
            return reg.test(value);
        },
        message:'用户名格式不正确'
    },
    password:{
        validator:function(value, param){
            var reg = /^.{6,30}$/;
            return reg.test(value);
        },
        message:'密码为6-30位字符'
    },
    phone:{
        validator:function(value, param){
            var reg = /^1\d{10}$/;
            return reg.test(value);
        },
        message:'请输入正确的手机号'
    },
    integer:{
        validator:function(value, param){
            var reg = /^\d+$/;
            return reg.test(value);
        },
        message:'只能输入数字'
    }
})